#include "bmpbutton.h"
#include <QApplication>
#include <QPainter>
#include <QDir>

#include "iconhelper.h"

BmpButton::BmpButton(QWidget * parent) 
    : QPushButton(parent), mHovered(false), mPressed(false), mBtnType(BUTTON4_TYPE)
{
    setWindowFlags(Qt::FramelessWindowHint);
    QObject::connect(this, SIGNAL(clicked()), this, SLOT(click()));
    mTxtToDraw.clear();

}

BmpButton::~BmpButton()
{

}

void BmpButton::setUI(BMPBUTTON_INFO& ui)
{
    resetPixMap();
    mTxtToDraw.clear();
    mUI = ui;
    if (mUI.rectBmp.width() == 0) {
        mUI.rectBmp = rect();
    }
    if (mUI.rectTxt.width() == 0) {
        mUI.rectTxt = mUI.rectBmp;
    }
    mUI.bmpPathHovered = mUI.bmpPathPressed;
    if (mUI.bmpPathPressed.isEmpty()) {
        mUI.bmpPathPressed = mUI.bmpPath;
    }

    QPushButton::setText(mUI.text);
    QFont font = this->font();
    font.setPointSize(mUI.fontSize);
    QPushButton::setFont(font);
}

void BmpButton::resetPixMap()
{

}

void BmpButton::setbmpPath(const QString& bmpPath, const QString& bmpPathPressed) 
{
    if(mUI.bmpPath != bmpPath){
        mUI.bmpPath = bmpPath;
    }

    if(mUI.bmpPathPressed != bmpPathPressed){
        mUI.bmpPathPressed = bmpPathPressed;
    }

    if(mUI.bmpPathHovered != bmpPathPressed){
        mUI.bmpPathHovered = bmpPathPressed;
    }

    mbmp = QPixmap();
    mbmpPressed = QPixmap();
    mbmpHovered  = QPixmap();

    update();
}

void BmpButton::paintEvent(QPaintEvent *pe)
{
    if (mUI.bmpPath.length() < 1 && mUI.icon < 1) {
        QPushButton::paintEvent(pe);
        return;
    }
    Q_UNUSED(pe);

    QPainter painter(this);
    painter.setRenderHint(QPainter::Antialiasing, true);

    QColor txtColor = mUI.txtColor;
    QPixmap realBmp;
    if(this->isEnabled()) {
        if(mPressed) {
             txtColor = mUI.txtColorPressed;
            if(mbmpPressed.isNull()) {
                if (mUI.icon > 0) {
                    mbmpPressed = IconHelper::getPixmap(txtColor, mUI.icon, mUI.fontSize, mUI.rectBmp.width(), mUI.rectBmp.height());
                } else {
                    if (mUI.bmpPathPressed == mUI.bmpPath) {
                        QImage myImage;//利用QImage的处理图片的功能
                        myImage.load(GetBMPPATH(mUI.bmpPathPressed));
                        myImage.invertPixels();
                        mbmpPressed = QPixmap();
                        mbmpPressed = mbmpPressed.fromImage(myImage);
                    } else {
                        mbmpPressed = QPixmap(GetBMPPATH(mUI.bmpPathPressed));
                    }
                }
            }
            realBmp = mbmpPressed;
        } else if (mHovered) {
            txtColor = mUI.txtColorPressed;
            if(mbmpHovered.isNull() ){
                if(mbmpPressed.isNull()) {
                    if (mUI.icon > 0) {
                        mbmpPressed = IconHelper::getPixmap(txtColor, mUI.icon, mUI.fontSize, mUI.rectBmp.width(), mUI.rectBmp.height());
                    } else {
                        mbmpPressed = QPixmap(GetBMPPATH(mUI.bmpPathPressed));
                    }
                }
                mbmpHovered = mbmpPressed;
            }
            realBmp = mbmpHovered;

        } else {
            if(mbmp.isNull()) {
                if (mUI.icon > 0) {
                    mbmp = IconHelper::getPixmap(txtColor, mUI.icon, mUI.fontSize, mUI.rectBmp.width(), mUI.rectBmp.height());
                } else {
                    mbmp = QPixmap(GetBMPPATH(mUI.bmpPath));
                }
            }
            realBmp = mbmp;
        }
    } else {
        if(mbmpX.isNull() && mUI.bmpPathX.size() > 0)
            mbmpX = QPixmap(GetBMPPATH(mUI.bmpPathX));

        if (mbmpX.isNull()){
            if(mbmp.isNull())
                mbmp = QPixmap(GetBMPPATH(mUI.bmpPath));
            realBmp = mbmp;
        }else{
            realBmp = mbmpX;
        }
        txtColor = QColor(100, 100, 100);
    }

    if (!realBmp.isNull())
        painter.drawPixmap(mUI.rectBmp, realBmp);

    if (!mIcon.isEmpty())
    {
        painter.save();
        if(mbmpIcon.isNull())
            mbmpIcon = QPixmap(GetBMPPATH(mIcon));
        painter.drawPixmap(mIconRt, mbmpIcon);
        painter.restore();
    }

    QString text = mUI.text;
    if (text.isEmpty()) {
        text = this->text();
    }

    if(!text.isEmpty() && mUI.icon == 0) {
        QFont font = this->font();
        font.setPointSize(mUI.fontSize);
        if (mUI.fontWeight > 0) {
            font.setWeight((QFont::Weight)mUI.fontWeight);
        }
        painter.setFont(font);
        painter.setPen(txtColor);
        painter.setOpacity(1.0);

        if (mUI.bCheckTxtLen) {
            if (mTxtToDraw.isEmpty()){
                mTxtToDraw = modifyDrawText(painter, mUI.rectTxt, mUI.flag, text);
            }
        }else{
            mTxtToDraw = text;
        }

        painter.drawText(mUI.rectTxt, mUI.flag, mTxtToDraw);
    }
}

void BmpButton::mousePressEvent(QMouseEvent * e)
{
    if (BTN_ON_DOWN == mBtnType) {
        setPressed(true);
        repaint();
        emit onMouseDown();

        setPressed(false);
        repaint();
        return;
    }
    if (mBtnType == CHECKBOX_TYPE){
        mHovered = !mHovered;
        emit clicked();
    }else{
        setPressed(true);
    }

    QPushButton::mousePressEvent(e);
}

void BmpButton::mouseReleaseEvent(QMouseEvent * e)
{
    if (BTN_ON_DOWN == mBtnType) {
        return;
    }
    if (mBtnType == BUTTON4_TYPE)
    {
        setPressed(false);
        QPushButton::mouseReleaseEvent(e);
    }
}

void BmpButton::setHovered(bool hovered)
{
    //mPressed = false;
    mHovered = hovered;
    update();
}

void BmpButton::setPressed(bool pressed)
{
    mPressed = pressed;
    update();
}

void BmpButton::click()
{	
    QString text = this->text();
    if (text.isEmpty()) {
        text = mUI.text;
    }

    if (text.isEmpty())	{
        text = mHint;
    }

    if (!text.isEmpty()) {
        emit sendKey(text);
    }

    if (mBtnType == BUTTON4_TYPE)
        setPressed(false);
}

void BmpButton::setIcon(const QString& iconPath, const QRect& iconRt)
{
    if (iconPath.size() <= 0) return;

    if(mIcon != iconPath){
        mIcon = iconPath;
        QPixmap tmp;
        //mbmpIcon.swap(tmp);
    }

    if (iconRt.width() > 0)
        mIconRt = iconRt;
    else
        mIconRt = mUI.rectBmp;
}

void BmpButton::setText(const QString& text, bool bUpdate)
{
    mUI.text = text;
    mTxtToDraw.clear();
    if (bUpdate){
        update();
    }
}

void BmpButton::setCheckBoxType(){
    mBtnType = CHECKBOX_TYPE;
    QObject::disconnect(this, SIGNAL(clicked()), this, SLOT(click()));
}

void BmpButton::setOnDownBtn()
{
    mBtnType = BTN_ON_DOWN;
    QObject::disconnect(this, SIGNAL(clicked()), this, SLOT(click()));
}
